package PLAYER;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.Socket;

/**
 * Created by Mirosław on 2015-01-16.
 */
public class MainThread extends Thread
{
    boolean connecting = false;
    boolean close;
    Socket socket = null;
    BufferedReader in = null;
    PrintWriter out = null;
    PlayerWindow PlayerWindow;
    ConnectingWindow Begin;
    String message;

    int port_nr;
    String host_name;
    String nickname;

    MainThread(int port_nr, String host_name, String nickname, ConnectingWindow Begin)
    {
        this.port_nr=port_nr;
        this.host_name=host_name;
        this.nickname=nickname;
        this.Begin = Begin;
    }




    public synchronized void run()
    {
        try
        {
            socket = new Socket(host_name, port_nr);
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            connecting = true;
        }
        catch (IOException ioe)
        {
            JOptionPane.showMessageDialog(null, "Unsuccesfull connection");
        }

        if(connecting)
        {
            while(true)
            {
                try
                {
                    message = in.readLine();
                    System.out.println(message);
                    String results[] = PlayerWindow.decoder(message);

                    if (results[0].equals("ServerS"))
                    {
                        if (results[1].equals("2"))
                        {
                            out.println("Quit");
                            JOptionPane.showMessageDialog(null, "There are to more players on server");
                            break;
                        }
                        else if(results[1].equals("1"))
                        {
                            continue;
                        }
                        else if(results[1].equals("0"))
                        {
                            out.println("Nick "+nickname);
                            JOptionPane.showMessageDialog(null, "You have to wait for other players");
                        }
                    }
                    else if(results[0].equals("I"))
                    {
                        Begin.dispose();
                        PlayerWindow = new PlayerWindow(Integer.parseInt(results[1]), Integer.parseInt(results[2]), nickname);
                        PlayerWindow.setVisible(true);
                        continue;
                    }
                    else if(results[0].equals("Cards"))
                    {
                        PlayerWindow.changeCardView(results);
                    }
                    else if(results[0].equals("U_Chips"))
                    {
                        PlayerWindow.changeChipsForAll(results);
                    }
                    else if(results[0].equals("DeletePlayer"))
                    {
                        PlayerWindow.deletePlayer(results);
                    }
                    else if(results[0].equals("Nick"))
                    {
                        PlayerWindow.setPlayersNick(results);
                    }
                }
                catch (IOException ioe)
                {
                    JOptionPane.showMessageDialog(null, "Error in connection");
                    break;
                }
            }
        }
    }
}
